Imports System.IO
Imports System.Xml.Serialization

#Region " SerializationHelper Class "

''' <summary>
''' Utility class that provides a number of serialization helper methods.
''' </summary>
''' <remarks></remarks>
Friend MustInherit Class SerializationHelper

#Region " Constructors "

    ''' <summary>
    ''' Prevents inheritance of this class.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
        MyBase.New()
    End Sub

#End Region

#Region " Serialization Methods "

    Public Shared Sub SerializeToFile(ByVal value As Object, ByVal type As Type, ByVal fileName As String)
        Dim objFileStream As FileStream = Nothing
        Try
            objFileStream = New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
            SerializationHelper.SerializeToStream(value, type, objFileStream)
        Finally
            If Not (objFileStream Is Nothing) Then objFileStream.Close()
        End Try
    End Sub

    Public Shared Sub SerializeToStream(ByVal value As Object, ByVal type As Type, ByVal stream As Stream)
        Dim objSerializer As XmlSerializer
        Dim objXmlWriter As Xml.XmlTextWriter
        ' prepare a writer so we use indented formatting
        objXmlWriter = New Xml.XmlTextWriter(stream, System.Text.Encoding.Unicode)
        objXmlWriter.Formatting = Xml.Formatting.Indented
        objXmlWriter.IndentChar = " "c
        objXmlWriter.Indentation = 2
        ' serialize the value
        objSerializer = New XmlSerializer(type)
        objSerializer.Serialize(objXmlWriter, value)
        objXmlWriter.Flush()
    End Sub

#End Region

#Region " Deserialization Methods "

    Public Shared Function Deserialize(ByVal type As Type, ByVal xml As String) As Object
        Dim objStream As MemoryStream
        Dim objWriter As StreamWriter
        Dim objValue As Object
        ' convert the xml into a stream
        objStream = New MemoryStream
        objWriter = New StreamWriter(objStream)
        objWriter.Write(xml)
        objWriter.Flush()
        objStream.Seek(0, SeekOrigin.Begin)
        ' deserialize the stream
        objValue = SerializationHelper.DeserializeFromStream(type, objStream)
        ' clean up
        objStream.Close()
        ' return the result
        Return objValue
    End Function

    Public Shared Function DeserializeFromFile(ByVal type As Type, ByVal fileName As String) As Object
        Dim objFileStream As FileStream = Nothing
        Try
            objFileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
            Return SerializationHelper.DeserializeFromStream(type, objFileStream)
        Finally
            If Not (objFileStream Is Nothing) Then objFileStream.Close()
        End Try
    End Function

    Public Shared Function DeserializeFromStream(ByVal type As Type, ByVal stream As Stream) As Object
        Dim objSerializer As XmlSerializer
        ' deserialize the value
        objSerializer = New XmlSerializer(type)
        Try
            Return objSerializer.Deserialize(stream)
        Catch ex As Exception
            MsgBox(ex.ToString)
            Throw
        End Try
    End Function

#End Region

End Class

#End Region